package com.rytong.tools.parserwidgets;

import java.util.HashMap;
import com.rytong.tools.utils.Utils;

public class LPWidgets {

    private String class_;
    private String init_;
    private String tag_;
    private String type_;
    protected HashMap<String, String> attrs_;

    protected LPWidgets() {
        attrs_ = new HashMap<String, String>();
    }

    final protected void setClass(String name) {
        class_ = name;
    }

    final protected void setInit(String name) {
        init_ = name;
    }

    final protected void setTag(String name) {
        tag_ = name;
    }

    final protected void setType(String name) {
        // TODO Auto-generated method stub
        type_ = name;
    }

    final protected void setAttr(String name, String value) {
        attrs_.put(name, value);
    }

    final protected Object getAttr(String name) {
        return attrs_.get(name);
    }

    final protected String getClassName() {
        return class_;
    }

    final protected String getInit() {
        return init_;
    }

    final protected String getTag() {
        return tag_;
    }

    final protected String getType() {
        return type_;
    }

    final protected int getFrontColor() {
        final String name = "FrontColor";
        String colorstr = "";
        try {
            colorstr = getValidColorString(getAttr(name).toString());
        } catch (Exception e) {
            // TODO Auto-generated catch block
            Utils.printException(e);
        }
        int color = Integer.parseInt(colorstr, 16);
        return color;
    }

    final protected int getFontSize() {
        try {
            final String name = "FontSize";
            String fs = getAttr(name).toString();
            return Integer.parseInt(fs);
        } catch (Exception e) {
            return 15;
        }
    }

    final protected boolean getBoolean(String name) {
        try {
            boolean b = false;
            final String value = getAttr(name).toString();
            if (value.equals("true")) {
                b = true;
            }
            return b;
        } catch (Exception e) {
            return false;
        }
    }

    /**
     * The valid color string is a 6 bit hex number. e.g., FFFFFF.
     * 
     * @param srcColorstr
     * @return
     */
    private static final String getValidColorString(final String srcColorstr) throws Exception {
        String colorstr = srcColorstr;
        if (colorstr.startsWith("#")) {
            colorstr = colorstr.substring(1);
        }

        if (colorstr.length() > 6) {
            int end = colorstr.length();
            int start = end - 6;
            colorstr = colorstr.substring(start, end);
        }

        return colorstr;
    }

}
